package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.tobato.fastdfs.domain.FileInfo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.yu.crm.domain.pojo.Img;
import com.yu.crm.mapper.ImgMapper;
import com.yu.crm.service.IImgService;
import com.yu.crm.utils.FileBase64Util;
import com.yxzz.common.config.file.FastDFSClientWrapper;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.DateHelpers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 图片表 服务实现类
 * </p>
 *
 * @author zds
 * @since 2021-03-22
 */
@Service
public class ImgService extends ServiceImpl<ImgMapper, Img> implements IImgService {

    @Value("${upload.basedir}")
    private String basedir;

    @Autowired
    private FastDFSClientWrapper dfsClient;

    @Autowired
    private ImgService imgService;

    /**
     * 批量上传图片(通过FastDFS管理文件存储路径)
     *
     * @param file
     * @return
     * @throws Exception
     */
    public List<Img> upload(MultipartFile file, String identityId) {
        List<Img> list = new ArrayList<>();
        try {
            //上传文件返回文件信息
            StorePath uploadFile = dfsClient.uploadFile(file);
            //获取上传的文件信息
            FileInfo info = dfsClient.queryFileInfo(uploadFile.getGroup(), uploadFile.getPath());
//            System.out.println(uploadFile.getGroup() + "===========================" + uploadFile.getPath() + "=========" + uploadFile.getFullPath());
            Img img = new Img();
            img.setFileUrl(uploadFile.getFullPath());
            img.setIdentityId(identityId);
            list.add(img);
            //批量添加附件
            if (DataHelpers.isNotEmpty(list) && list.size() > 0) {
                imgService.saveBatch(list);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 批量上传图片(通过FastDFS管理文件存储路径)
     *
     * @param ss
     * @return
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public List<Img> upload(String ss, String identityId) {
        MultipartFile multipartFile = FileBase64Util.base64ToMultipartFile(ss);
        String fileName = identityId + ".jpg";
        File file = FileBase64Util.base64ToFile(ss, fileName);
        List<Img> list = new ArrayList<>();
        try {
            //上传文件返回文件信息
//            StorePath uploadFile = dfsClient.uploadFile(file);
            //获取上传的文件信息
//            FileInfo info = dfsClient.queryFileInfo(uploadFile.getGroup(), uploadFile.getPath());
//            System.out.println(uploadFile.getGroup() + "===========================" + uploadFile.getPath() + "=========" + uploadFile.getFullPath());
            multipartFile.transferTo(file);
            //获取系统文件分隔符
            String separator = System.getProperty("file.separator");
            //文件全路径
            String filePath = basedir + separator + fileName;
            Img img = new Img();
            img.setFileUrl(filePath);
            img.setIdentityId(identityId);
            list.add(img);
            //批量添加附件
            if (DataHelpers.isNotEmpty(list) && list.size() > 0) {
                imgService.saveBatch(list);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<Img> upload2(MultipartFile[] file) {
        //String userId = userCacheManager.getCurrentUser().getUserId();

        List<Img> list = new ArrayList<>();
        try {
            for (MultipartFile mf : file) {
                System.out.println(mf.getInputStream() + "=============" + mf.getBytes()+"====="+mf.getResource()+"======"+ mf.getClass());
                System.out.println(mf.getOriginalFilename() + "=======" + mf.getContentType() + "======" + mf.getSize() + "========" + mf.getName());
//                System.out.println(mf.getInputStream() + "=============" + mf.getBytes()+"====="+mf.getResource()+"======"+ mf.getClass());
                //上传文件返回文件信息
                StorePath uploadFile = dfsClient.uploadFile(mf);
//                if (null!=uploadFile) {
                //获取上传的文件信息
                FileInfo info = dfsClient.queryFileInfo(uploadFile.getGroup(), uploadFile.getPath());
                String fileUrl = "D:\\java\\img\\" + mf.getOriginalFilename();
                Img img = new Img();
                img.setFileUrl(fileUrl);
//                    img.setFileUrl(uploadFile.getFullPath());
                img.setFileName(mf.getOriginalFilename());
                img.setFileSize(mf.getSize());
                img.setCreateTime(DateHelpers.now());
//                img.setIdentityId(identityId);
                list.add(img);
//                }
            }
            //批量添加附件
            if (DataHelpers.isNotEmpty(list) && list.size() > 0) {
                imgService.saveBatch(list);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

}
